<?php
    class SettingsController extends AppController
    {
		var $name = 'Settings';
		var $helpers = array('Html', 'Form', 'Javascript', 'Time');
		var $uses = array('Setting','Upcoming');
		//var $scaffold;
		function admin_index()
		{
			
			$api_key = $this->Setting->field("value", "Setting.key='upcoming_api_key'");
			if ($api_key) {
				$this->Upcoming->options["api_key"] = $api_key;
				$this->Upcoming->upc =& new Services_Upcoming($this->Upcoming->options, $this->Upcoming->errorArray);
				$auth_url = $this->Upcoming->upc->getAuthUrl();

				$this->set('auth_url',$auth_url);
			}
			
			
			$this->set('data',$this->Setting->findAll());
						
		} 
		
		function admin_authtoken($frob)
		{
			
			$api_key = $this->Setting->field("value", "Setting.key='upcoming_api_key'");
			if ($api_key && $frob) {
				$this->Upcoming->options["api_key"] = $api_key;
				$this->Upcoming->upc =& new Services_Upcoming($this->Upcoming->options, $this->Upcoming->errorArray);
				$xml = $this->Upcoming->upc->callMethod('auth.getToken', array('frob' => $frob));
		        $token = (string)$xml['token']['token'];
				
				$this->data["Setting"]["value"] = $token;
				
				if ($token && $this->Setting->save($this->data))
				{
					$this->Session->setFlash('Auth token updated.');
					$this->redirect('/admin/settings/index');
					exit();
				}
				else {
					$this->Session->setFlash('Error updating auth token.<pre>' . print_r($this->Upcoming->errorArray,true) . '</pre>');
					$this->redirect('/admin/settings/index');
					exit();
				}
				
			}
			
		}
		
		function admin_view($id)
		{
			$this->layout = 'admin';
		     $this->Setting->id = $id;
			$this->set('data', $this->Setting->read());
		}

		/** \brief add a single model item
			\author  __MyCompanyName__
			\date 2007-04-18

			\sa
		**/
		function admin_add()
		{
			$this->layout = 'admin';
			if (empty($this->params['data']))
			{
				$this->render();
			}
			else
			{
				if ($this->Setting->save($this->params['data']))
				{
					$this->Session->setFlash('Your Setting has been saved');
					$this->redirect('/admin/settings/index');
				}
				else
				{
					$this->set('data', $this->params['data']);
					$this->validateErrors($this->Setting);
					$this->render();
				}
			}
		} // END function admin_add

		/** \brief delete a single model item
			\author  __MyCompanyName__
			\date 2007-04-18
			\param id id of model item delete
			\sa
		**/
		function admin_delete($id) {
			$this->layout = 'admin';
			if ($this->Setting->del($id))
			{
				$this->Session->setFlash('The Setting with id: '.$id.' has been deleted.');
				$this->redirect('/admin/settings/index');
			}
		} // END function admin_delete

		/** \brief edit a single model item
			\author  __MyCompanyName__
			\date 2007-04-18
			\param id id of model item to edit
			\sa
		**/
		function admin_edit($id=null) 	{
			//$this->layout = 'admin';
			if (empty($this->params['data']))
			{
				$this->Setting->setId($id);
				$this->params['data'] = $this->Setting->read();
				$this->set('data', $this->params['data']);	
				$this->render();
			}
			else
			{
				$this->Setting->set($this->params['data']);
				if ( $this->Setting->save())
				{
					
					$this->Session->setFlash('Your Setting has been updated.');
					$this->redirect('/admin/settings/index');
				}
				else
				{
					$this->set('data', $this->params['data']);
					$this->validateErrors($this->Setting);
					$this->render();
				}
			}
		} // END function admin_edit
		//@}
    }
?>